package com.interview.javabase.algorithm;

/**
 * @author: zsg
 * @description:
 * @date: 2019/8/17 11:15
 * @modified:
 */
public class HandWriteCircleQueue {

    public static void main(String[] args) {
        ArrayCircleQueue arrayQueue = new ArrayCircleQueue(10);
        arrayQueue.pull(1);
        arrayQueue.pull(2);
        arrayQueue.pull(3);
        arrayQueue.pull(4);
        arrayQueue.pull(5);
        arrayQueue.pull(6);
        arrayQueue.pull(7);
        arrayQueue.pull(8);
        arrayQueue.pull(9);
        arrayQueue.pull(10);

        System.out.println(arrayQueue.get());
        System.out.println(arrayQueue.get());
        System.out.println(arrayQueue.get());
        System.out.println(arrayQueue.get());
        System.out.println(arrayQueue.get());

        arrayQueue.pull(11);
        arrayQueue.pull(12);
        arrayQueue.pull(13);
        arrayQueue.pull(14);
//        arrayQueue.showQueue();

    }

}

class ArrayCircleQueue {
    private int maxSize;

    private int front;

    private int rear;

    private int[] arr;

    public ArrayCircleQueue(int maxSize) {
        this.maxSize = maxSize + 1;
        this.front = 0;
        this.rear = 0;
        this.arr = new int[maxSize + 1]; // 会预留一个空的位置
    }

    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    public boolean isEmpty() {
        return rear == front;
    }

    public int get() {
        if (isEmpty()) {
            System.out.println("队列没有数据");
            return -1;
        }

        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    public boolean pull(int value) {
        if (isFull()) {
            System.out.println("队列已经满了");
            return false;
        }

        arr[rear] = value;
        rear = (rear + 1) % maxSize;
        return true;
    }

    public void showQueue() {
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }

    public int getMaxSize() {
        return maxSize;
    }

    public ArrayCircleQueue setMaxSize(int maxSize) {
        this.maxSize = maxSize;
        return this;
    }

    public int getFront() {
        return front;
    }

    public ArrayCircleQueue setFront(int front) {
        this.front = front;
        return this;
    }

    public int getRear() {
        return rear;
    }

    public ArrayCircleQueue setRear(int rear) {
        this.rear = rear;
        return this;
    }

    public int[] getArr() {
        return arr;
    }

    public ArrayCircleQueue setArr(int[] arr) {
        this.arr = arr;
        return this;
    }
}